package com.eqdd.yiqidian.ui.gongneng.renshi.quanxian;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.RecyclerViewRefreshCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.DimissionLeaderListResultBean;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.utils.RefreshUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 17-11-14  上午8:47.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class DimissionLeaderListActivity extends BaseActivity {

    private RecyclerViewRefreshCustom dataBinding;
    private SlimAdapterEx slimAdapterEx;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview_refresh);
        initTopTitleBar(View.VISIBLE, "离职审批人列表");
        initTopRightText("添加", v -> {
            startActivityForResult(DimissionLeaderSetActivity.class, null, Config.DIMISSION_LEADER_SET);
        });
    }

    @Override
    public void initData() {


        RefreshUtil.openRefresh(dataBinding.coolRefreshView, coolRefreshView -> {
            refresh();
        });
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_44, new SlimInjector<DimissionLeaderListResultBean>() {
            @Override
            public void onInject(DimissionLeaderListResultBean data, IViewInjector injector) {

                StringBuilder stringBuilder = new StringBuilder();
                for (DimissionLeaderListResultBean.ListBean listBean : data.getList()) {
                    stringBuilder.append(listBean.getDepartment() + " ");
                }
                injector.text(R.id.tv_upper_content, data.getChecker())
                        .text(R.id.tv_down_content, stringBuilder.toString());
            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
        dataBinding.coolRefreshView.setRefreshing(true);
    }

    private void refresh() {
        OkGo.<HttpResult<List<DimissionLeaderListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.DIMISSION_LEADER_LIST)
                .params("companyId", AppConstent.user.getCompanyId())
                .execute(new JsonCallBack<HttpResult<List<DimissionLeaderListResultBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpResult<List<DimissionLeaderListResultBean>>> response) {
                        HttpResult<List<DimissionLeaderListResultBean>> httpResult = response.body();
                        if (httpResult.getStatus() == 200) {
                            slimAdapterEx.updateData(httpResult.getItems());
                        }
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }

                    @Override
                    public void onError(Response<HttpResult<List<DimissionLeaderListResultBean>>> response) {
                        super.onError(response);
                        dataBinding.coolRefreshView.setRefreshing(false);
                    }
                });
    }

    @Override
    public void setView() {


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            dataBinding.coolRefreshView.setRefreshing(true);
        }
    }
}
